package bean.tasktype;

import java.io.Serializable;
import java.util.List;

import dao.tasktype.TaskTypeDAO;
import entity.tasktype.TaskType;

import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.criteria.CriteriaQuery;

/*@ManagedBean@ConversationScoped
@ViewScoped*/

@Named
@Stateful
@ConversationScoped
public class TaskTypeBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Inject
	private TaskTypeDAO taskTypeDAO;
	
	private Long id;
	
	   @Inject
	   private Conversation conversation;
	
	private TaskType taskType;
	
	public void load() {
		if (null == id) {
			taskType = new TaskType();
		} else {
			taskType = taskTypeDAO.findById(id, false);
		}
	}
	
	public String save() {
		taskTypeDAO.makePersistent(taskType);
		//taskTypeDAO.flush();
		return "/content/tasktype/list?faces-redirect=true";
	}
	
	public String delete()
	   {
	      this.conversation.end();

	      try
	      {
	         this.entityManager.remove(findById(getId()));
	         this.entityManager.flush();
	         return "/content/tasktype/list?faces-redirect=true";
	      }
	      catch (Exception e)
	      {
	         //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
	    	  return "/content/error?faces-redirect=true";
	      }
	   }
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public TaskType getTaskType() {
		return taskType;
	}
	
	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

	public List<TaskType> getAll()
	   {

	      CriteriaQuery<TaskType> criteria = this.entityManager.getCriteriaBuilder().createQuery(TaskType.class);
	      return this.entityManager.createQuery(criteria.select(criteria.from(TaskType.class))).getResultList();
	   }
	
	
    @PersistenceContext(type = PersistenceContextType.EXTENDED)
	private EntityManager entityManager;
    
    public String create()
    {
       this.conversation.begin();
       return "/content/tasktype/list?faces-redirect=true";
    }
	
    public String update()
    {
       this.conversation.end();

       try
       {
          if (this.id == null)
          {
             this.entityManager.persist(this.taskType);
             return "/content/tasktype/list?faces-redirect=true";
          }
          else
          {
             this.entityManager.merge(this.taskType);
        	 this.entityManager.flush();
             return "/content/tasktype/list?faces-redirect=true";
          }
       }
       catch (Exception e)
       {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
          return null;
       }
    }
    
    public void retrieve()
    {

       if (FacesContext.getCurrentInstance().isPostback())
       {
          return;
       }

       if (this.conversation.isTransient())
       {
          this.conversation.begin();
       }

       if (this.id == null)
       {
          this.taskType = new TaskType();
       }
       else
       {
          this.taskType = findById(getId());
       }
    }
    
	public TaskType findById(Long id)
	{
	      return this.entityManager.find(TaskType.class, id);
	}
	
	@Resource
	private SessionContext sessionContext;

	public Converter getConverter()
	{

	   final TaskTypeBean ejbProxy = this.sessionContext.getBusinessObject(TaskTypeBean.class);

	      return new Converter()
	      {

	         @Override
	         public Object getAsObject(FacesContext context, UIComponent component, String value)
	         {
	            return ejbProxy.findById(Long.valueOf(value));
	         }

	         @Override
	         public String getAsString(FacesContext context, UIComponent component, Object value)
	         {

	            if (value == null)
	            {
	               return "";
	            }

	            return String.valueOf(((TaskType) value).getId());
	         }
	      };
	   }
}
